package com.gruuf.done.repositories;

import com.gruuf.done.model.Task;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Put some description
 */
public class TasksRepository {

    public void updateTask(Task task) {
        PersistenceManager pm = PMF.getInstance();
        pm.makePersistent(task);
    }

    public List<Task> readTasks(String userId) {
        PersistenceManager pm = PMF.getInstance();
        Query query = pm.newQuery(Task.class);
        query.setFilter("userId == userIdParam ");
        query.setOrdering("created desc");
        query.declareParameters("String userIdParam");
        return (List<Task>) query.execute(userId);
    }

    public Task readTask(String key) {
        PersistenceManager pm = PMF.getInstance();
        return pm.getObjectById(Task.class, key);
    }

    public Set<String> readTypes(String userId) {
        List<Task> tasks = readTasks(userId);
        Set<String> result = new HashSet<String>();
        for (Task task : tasks) {
            if (!result.contains(task.getType())) {
                result.add(task.getType());
            }
        }
        return result;
    }

}
